package com.ffox.utils;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.sql.Types;
import java.util.Collections;

public class MybatisGeneratorUtil {

    public static void main(String[] args) {
        FastAutoGenerator.create("jdbc:mysql://localhost:3306/information_platform", "root", "123456")
                .globalConfig(builder -> {
                    builder.author("freefox") // 设置作者
                            .disableOpenDir() // 生成后不打开目录
                            .outputDir("D:\\HMOMEWORK\\Graduation Project\\InformationPlatform\\information-server\\info-parent\\info-entity\\src\\main\\java"); // 指定输出目录
                })
                .dataSourceConfig(builder ->
                        builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                            int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                            if (typeCode == Types.SMALLINT) {
                                return DbColumnType.INTEGER; // 自定义类型转换
                            }
                            return typeRegistry.getColumnType(metaInfo);
                        })
                )
                .packageConfig(builder ->
                        builder.parent("com.ffox") // 父包名
                                .entity("entity") // 实体类包名
                )
                .strategyConfig(builder ->
                        builder.addInclude("homework_problem","homework_result","homework_problem_file","homework_result_file")
                                .entityBuilder() // 实体类配置
                                .enableLombok() // 使用 Lombok
                                .disableSerialVersionUID() // 不生成 serialVersionUID
                                .enableTableFieldAnnotation() // 字段上添加 @TableField 注解
                                // 明确指定不生成其他文件
                                .controllerBuilder().disable()
                                .serviceBuilder().disable()
                                .mapperBuilder().disable()
                )
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();
    }
}